package com.yx.system.controller;

import java.util.List;
import java.util.Map;

import com.yx.common.annotation.LoginUser2;
import com.yx.common.utils.PageUtils;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.common.validator.Assert;
import com.yx.system.entity.SysHanwuBookInfo;
import com.yx.system.entity.SysUser;
import com.yx.system.service.SysHanwuBookInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



/**
 * 寒武第三方图书详情
 * 
 * @author luo
 * @email www.baidu.com
 * @date 2018-07-30 11:15:14
 */
@RestController
@RequestMapping("sys/hanwubookinfo")
public class SysHanwuBookInfoController {
	@Autowired
	private SysHanwuBookInfoService hanwuBookInfoService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:hanwubookinfo:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<SysHanwuBookInfo> hanwuBookInfoList = hanwuBookInfoService.queryList(query);
		int total = hanwuBookInfoService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(hanwuBookInfoList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:hanwubookinfo:info")
	public R info(@PathVariable("id") Integer id){
		SysHanwuBookInfo hanwuBookInfo = hanwuBookInfoService.queryObject(id);
		
		return R.ok().put("hanwuBookInfo", hanwuBookInfo);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:hanwubookinfo:save")
	public R save(@RequestBody SysHanwuBookInfo hanwuBookInfo){
		hanwuBookInfoService.save(hanwuBookInfo);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:hanwubookinfo:update")
	public R update(@RequestBody SysHanwuBookInfo hanwuBookInfo){
		hanwuBookInfoService.update(hanwuBookInfo);
		
		return R.ok();
	}
	
	/*
	* 删除导入图书
	*/
	@RequestMapping("/delete")
	@RequiresPermissions("sys:hanwubookinfo:delete")
	public R delete(@RequestBody String[] ids){
		Thread thread = new Thread(){
			public void run(){
				hanwuBookInfoService.deleteBatch(ids);
			}
		};
		thread.start();
		return R.ok("正在清理数据，请在几分钟后刷新查看");
	}

	/*
	* 导入图书
	*/
	@RequestMapping("/put")
	@RequiresPermissions("sys:hanwubookinfo:put")
	public R put(@RequestBody SysHanwuBookInfo hanwuBookInfo, @LoginUser2 SysUser sysUser){
		Assert.isNull(hanwuBookInfo.getCid(),"请选择图书分类");
		Thread thread = new Thread(){
			public void run(){
				hanwuBookInfoService.put(hanwuBookInfo,sysUser);
			}
		};
		thread.start();
		hanwuBookInfo.setStatus(2);
		hanwuBookInfoService.update(hanwuBookInfo);
		return R.ok("图书导入需要时间，请在几分钟后刷新页面查看导入状态");
	}



}
